package com.itheima.easy.adapter.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.itheima.easy.adapter.CommonPayAdapter;
import com.itheima.easy.constant.TradingConstant;
import com.itheima.easy.enums.TradingEnum;
import com.itheima.easy.exception.ProjectException;
import com.itheima.easy.handler.CommonPayHandler;
import com.itheima.easy.pojo.Trading;
import com.itheima.easy.service.ITradingService;
import com.itheima.easy.utils.RegisterBeanHandler;
import com.itheima.easy.vo.RefundRecordVo;
import com.itheima.easy.vo.TradingVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName CommonPayAdapterImpl.java
 * @Description 基础交易适配
 */
@Slf4j
@Component
public class CommonPayAdapterImpl implements CommonPayAdapter {

    @Autowired
    RegisterBeanHandler registerBeanHandler;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    ITradingService tradingService;

    private static Map<String,String> commonPayHandlers =new HashMap<>();

    static {
        commonPayHandlers.put(TradingConstant.TRADING_CHANNEL_ALI_PAY,"aliCommonPayHandler");
        commonPayHandlers.put(TradingConstant.TRADING_CHANNEL_WECHAT_PAY,"wechatCommonPayHandler");
    }

    @Override
    public TradingVo queryTrading(TradingVo tradingVo) throws ProjectException {
        //从IOC容器中找到AppPayHandler实现
        String commonPayHandlerString = commonPayHandlers.get(tradingVo.getTradingChannel());
        CommonPayHandler commonPayHandler = registerBeanHandler
            .getBean(commonPayHandlerString, CommonPayHandler.class);
        //queryTrading查询订单结果
        return commonPayHandler.queryTrading(tradingVo);
    }

    /**
     * 申请退款
     * @param tradingVo 交易单
     * @return
     * @throws ProjectException
     */
    public TradingVo refundTrading(TradingVo tradingVo) throws ProjectException {
        //根据渠道获得bean名称
        String commonPayHandlerString = commonPayHandlers.get(tradingVo.getTradingChannel());

        //从IOC容器中找到对应的bean
        CommonPayHandler commonPayHandler = registerBeanHandler.getBean(commonPayHandlerString, CommonPayHandler.class);

        //refundTrading处理退款
        return commonPayHandler.refundTrading(tradingVo);
    }

    @Override
    public RefundRecordVo queryRefundTrading(RefundRecordVo refundRecordVo) throws ProjectException {
        String commonPayHandlerString = commonPayHandlers.get(refundRecordVo.getTradingChannel());
        CommonPayHandler commonPayHandler = registerBeanHandler
                .getBean(commonPayHandlerString, CommonPayHandler.class);
        return commonPayHandler.queryRefundTrading(refundRecordVo);
    }

    @Override
    public TradingVo closeTrading(TradingVo tradingVo) {
        String commonPayHandlerString = commonPayHandlers.get(tradingVo.getTradingChannel());
        CommonPayHandler commonPayHandler = registerBeanHandler
                .getBean(commonPayHandlerString, CommonPayHandler.class);
        return commonPayHandler.closeTrading(tradingVo);
    }

    @Override
    public String queryQrCode(TradingVo tradingVo) {
        Trading trading = tradingService.findTradByProductOrderNo(tradingVo.getProductOrderNo());
        //4、构建支付二维码
        BufferedImage image = QrCodeUtil.generate(trading.getPlaceOrderMsg(), 480, 480);
        //输出流
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", stream);
        } catch (IOException e) {
            throw new ProjectException(TradingEnum.TRAD_QRCODE_FAIL);
        }
        return Base64.encode(stream.toByteArray());
    }

    @Override
    public TradingVo downLoadBill(TradingVo tradingVo) {
        String commonPayHandlerString = commonPayHandlers.get(tradingVo.getTradingChannel());
        CommonPayHandler commonPayHandler = registerBeanHandler
                .getBean(commonPayHandlerString, CommonPayHandler.class);
        return commonPayHandler.downLoadBill(tradingVo);
    }

}
